package com.scau.bookstore.api.controller;

import com.scau.bookstore.api.annotation.LoginAuthToken;
import com.scau.bookstore.api.util.JwtUtil;
import com.scau.bookstore.common.entity.PageResult;
import com.scau.bookstore.common.entity.Result;
import com.scau.bookstore.common.entity.StatusCode;
import com.scau.bookstore.common.pojo.Address;
import com.scau.bookstore.contract.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/address")
public class AddressController {

	@Autowired
	private AddressService addressService;

	/**
	 * 根据token查询收货地址
	 */
	@LoginAuthToken
	@RequestMapping(value="/info",method= RequestMethod.GET)
	public Result findByToken(HttpServletRequest request){
		String token = request.getHeader("X-Token");// 从 http 请求头中取出 token
		Long userId = JwtUtil.getIdByToken(token);
		return new Result(true,StatusCode.OK,"查询成功",addressService.findByUserId(userId));
	}

	/**
	 * 根据token更新收货地址
	 */
	@LoginAuthToken
	@RequestMapping(value="/update",method= RequestMethod.POST)
	public Result updateByToken(@RequestBody Address address, HttpServletRequest request){
		String token = request.getHeader("X-Token");// 从 http 请求头中取出 token
		Long userId = JwtUtil.getIdByToken(token);
		address.setUserId(userId);
		addressService.update(address);
		return new Result(true,StatusCode.OK,"更新成功");
	}

	/**
	 * 返回一个地址
	 */
	@GetMapping("/findByUserId/{userId}")
	public Result findByUserId(@PathVariable Long userId){
		return new Result(true, StatusCode.OK,"查询成功",addressService.findByUserId(userId));
	}
	
	/**
	 * 查询全部数据
	 * @return
	 */
	@RequestMapping(method= RequestMethod.GET)
	public Result findAll(){
		return new Result(true, StatusCode.OK,"查询成功",addressService.findAll());
	}
	
	/**
	 * 根据ID查询
	 * @param id ID
	 * @return
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.GET)
	public Result findById(@PathVariable Long id){
		return new Result(true,StatusCode.OK,"查询成功",addressService.findById(id));
	}


	/**
	 * 分页+多条件查询
	 * @param searchMap 查询条件封装
	 * @param page 页码
	 * @param size 页大小
	 * @return 分页结果
	 */
	@RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
	public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
		Page<Address> pageList = addressService.findSearch(searchMap, page, size);
		return  new Result(true,StatusCode.OK,"查询成功",  new PageResult<Address>(pageList.getTotalElements(), pageList.getContent()) );
	}

	/**
     * 根据条件查询
     * @param searchMap
     * @return
     */
    @RequestMapping(value="/search",method = RequestMethod.POST)
    public Result findSearch( @RequestBody Map searchMap){
        return new Result(true,StatusCode.OK,"查询成功",addressService.findSearch(searchMap));
    }
	
	/**
	 * 增加
	 * @param address
	 */
	@RequestMapping(method=RequestMethod.POST)
	public Result add(@RequestBody Address address){
		addressService.add(address);
		return new Result(true,StatusCode.OK,"增加成功");
	}
	
	/**
	 * 修改
	 * @param address
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.PUT)
	public Result update(@RequestBody Address address, @PathVariable Long id ){
		address.setId(id);
		addressService.update(address);		
		return new Result(true,StatusCode.OK,"修改成功");
	}
	
	/**
	 * 删除
	 * @param id
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.DELETE)
	public Result delete(@PathVariable Long id ){
		addressService.deleteById(id);
		return new Result(true,StatusCode.OK,"删除成功");
	}
	
}
